package com.warm.micro.common.exception;

import com.warm.micro.common.vo.Resp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(CommonException.class)
    @ResponseBody
    public Resp serviceExceptionHandler(HttpServletRequest req, CommonException e){

        log.error("CommonException, code:{}, message", e.getCode(), e.getMessage());

        return Resp.commonException(e);
    }

    /**
     * 处理空指针的异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =NullPointerException.class)
    @ResponseBody
    public Resp exceptionHandler(HttpServletRequest req, NullPointerException e){
        log.error("发生空指针异常！原因是:{}", e);
        return Resp.nullException(e);
    }

    /**
     * 其他异常可自定义添加处理方式
     */
}